{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2f09e3f6-a94d-4a0f-a765-e5bad599234d",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a53c6d8-b10c-4f79-bbb4-930bf4bfc726",
   "metadata": {},
   "source": [
    "ndarray中的元素都是同一种类型，不是则自动向下转换"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62410c29-f78e-4425-b153-fa5154437073",
   "metadata": {},
   "source": [
    "# ndarray中的各种属性与函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "090b78b8-8fbd-4f07-b607-bab238dcd5c7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_list = [1,2,3,4,5]\n",
    "fang_array = np.array(fang_list)\n",
    "fang_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cf729bee-4002-4a7b-82d0-aef42ba17bdc",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(fang_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8431396c-3c40-4ebd-a7d2-529cbadc1120",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int64')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.dtype #查看数组的数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2fb3e9d3-d6e5-48a0-b4d0-cb51308bb4e3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array2 = np.array([1,2,3,4,5.0])\n",
    "fang_array2.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e7c661c5-854b-4132-959d-097e6540d0d7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.itemsize #一个元素占多少个字节(8bit)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "aa7687f0-ec03-4a01-80a6-feb4b744010a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5,)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "40e6efe1-118c-4ef4-811c-22618a84eeef",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b3d20b81-e402-461c-9b33-3e1b63c91379",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.ndim #查看数组是几维的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "15f3ddcb-46d7-4529-bfb4-ed99d6116b23",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.fill(0) #将数组的数据全部设置为\n",
    "fang_array"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f25e3f8f-a05f-4f77-870e-68fd2b8fcadf",
   "metadata": {},
   "source": [
    "# 切片与索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "7b7ce281-9804-4af3-b348-ea5ee87ec0c6",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_list = [1,2,3,4,5]\n",
    "fang_array = np.array(fang_list)\n",
    "fang_array[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "29768981-a0ab-4326-849b-8161b49998e5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array[1:3] #索引方式和python内置函数相同"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "de3111a1-4505-4527-bce6-9f55ee6f4eb1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 5])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array[-2:]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
